﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using SocialIn.Core.Data;
using System.Data;
using System.Web;
using System.Web.Security;
using SocialIn.Biz.Utils;

namespace SocialIn.Biz
{
    public sealed class SearchUserInfo
    {
        ConnectionStringSettings _BaseDBCon = null;
        SendMailProvider mailProvider;

        public SearchUserInfo()
        {
            _BaseDBCon = ConfigurationManager.ConnectionStrings[ConfigName.DBConfig];
            mailProvider = new SendMailProvider();
        }

        /// <summary>
        /// 아이디 찾기
        /// </summary>
        /// <param name="emailAddress"></param>
        /// <returns></returns>
        public bool SearchUserID(string emailAddress)
        {
            bool result = false;
            string query = string.Format(" SELECT UserID, UserName, Password FROM Member WHERE EMAIL = '{0}' ", emailAddress.Trim());
            
            SqlHelper sql = new SqlHelper(_BaseDBCon.ProviderName, _BaseDBCon.ConnectionString);
            Result<DataTable> dtResult = sql.ExecuteDataTable(query);

            if (dtResult.ReturnValue != null && dtResult.ReturnValue.Rows.Count == 1)
            {
                // 메일 발송
                mailProvider.Send(new SendMailInfo()
                {
                    Subject = "소셜인 회원님의 아이디를 보냅니다.",
                    Content = string.Format(@"회원님의 아이디는 '{0}'입니다.", dtResult.ReturnValue.Rows[0]["UserID"].ToString()), 
                    //Content = string.Format(Resources.MailResource.ResourceManager.GetString("Join_Subject"), dtResult.ReturnValue.Rows[0]["UserID"].ToString(), invitationkey),
                    To = new MailAddress() { Email = emailAddress, DisplayName = dtResult.ReturnValue.Rows[0]["UserName"].ToString() }
                });

                result = true;
            }

            return result;
        }


        /// <summary>
        /// 비밀번호 변경
        /// </summary>
        /// <param name="userID"></param>
        /// <param name="emailAddress"></param>
        /// <returns></returns>
        public bool SearchUserPWD(string userID, string emailAddress)
        {
            bool result = false;
            string query = string.Format(" SELECT UserID, UserName, Password FROM Member WHERE EMAIL = '{0}' AND UserID = '{1}' ", emailAddress.Trim(), userID.Trim());

            SqlHelper sql = new SqlHelper(_BaseDBCon.ProviderName, _BaseDBCon.ConnectionString);
            Result<DataTable> dtResult = sql.ExecuteDataTable(query);

            if (dtResult.ReturnValue != null && dtResult.ReturnValue.Rows.Count == 1)
            {
                string strHashedPassword = FormsAuthentication.HashPasswordForStoringInConfigFile("1234", "sha1");
                string updateQuery = string.Format(" UPDATE Member SET Password = '{0}' WHERE EMAIL = '{1}' AND UserID = '{2}' ", strHashedPassword, emailAddress.Trim(), userID.Trim());

                // 메일 발송
                mailProvider.Send(new SendMailInfo()
                {
                    Subject = "소셜인 회원님의 비밀번호를 초기화하여 보냅니다.",
                    Content = string.Format(@"회원님의 비밀번호를 다음과 같이 초기화하여 보냅니다.<br /> 초기화 비밀번호 : '{0}' ", "1234"),
                    //Content = string.Format(Resources.MailResource.ResourceManager.GetString("Join_Subject"), dtResult.ReturnValue.Rows[0]["UserID"].ToString(), invitationkey),
                    To = new MailAddress() { Email = emailAddress, DisplayName = dtResult.ReturnValue.Rows[0]["UserName"].ToString() }
                });

                result = true;
            }

            return result;
        }



    }
}
